package me.jiatao.wan.test

import androidx.paging.PagingSource
import androidx.paging.PagingState

/**
 * @Author: taojia
 * @CreateDate: 2021/8/25 11:25 上午
 * @Version: 1.0
 * @Description:
 */

class TestPagingSource : PagingSource<Int, String>() {
    private val startIndex = 1
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, String> {
        val pos = params.key ?: startIndex

        val start: Int = pos * 100
        val end: Int = start + 10
        val listString = (start..end).map {
            "Index: $it"
        }

        return LoadResult.Page(
            listString,
            if (pos <= startIndex) null else pos - 1,
            pos + 1
        )
    }

    /**
     * PagingAdapter调用refresh()时回调，获取刷新时的key
     * 如果是按页加载数据，这时应该返回默认初始页码
     */
    override fun getRefreshKey(state: PagingState<Int, String>): Int? {
        println("getRefreshKey")
        return state.anchorPosition
    }

}